package com.aierkeji.call.cms.dao.jpa.custom.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.util.CollectionUtils;

import com.aierkeji.call.cms.dao.jpa.custom.AkjCallUserDaoCustom;
import com.aierkeji.call.cms.entity.vo.AkjCallUserVo;
import com.aierkeji.call.cms.util.PageUtils;
import com.aierkeji.call.cms.util.StringUtils;

public class AkjCallUserDaoCustomImpl implements AkjCallUserDaoCustom {

	@PersistenceContext
	private EntityManager em;

	@Autowired
	HibernateTemplate hibernateTemplate;

	@Override
	public Page<AkjCallUserVo> getUserList(Long leaderId, Map<String, Object> map, int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		String name = (String) map.get("name");
		String isDelete = (String) map.get("isDelete");
		Long admin_id = (Long)map.get("admin_id");
		Long createdBy = (Long)map.get("createdBy");
		String manager = (String)map.get("manager");
		String sql = "select new com.aierkeji.call.cms.entity.vo.AkjCallUserVo(t.id as id,t.name as name,t.account as count,t.userNo as userNo,t.gender as gender,t.birth as birth,tp.name as position,te.name as enterprise ,t.updatedAt as updatedAt,t.createdAt as createdAt ,tu.id as updatedBy,tu.name as updatedName,"
				+ " t.leaderId, lu.name as leaderName,t.approvedAt	as approvedAt ,t.isDelete as isDelete )from AkjCallUser t  left join t.position tp left join t.enterprise te left join t.updatedBy tu"
				+ " left join AkjCallUser lu on lu.id = t.leaderId   "
				+ "where 1=1   ";
		String countslq = "select count(1) from AkjCallUser t where 1=1  ";
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		if (!StringUtils.isEmpty(name)) {
			sql += " and (t.name like :name or t.account like :name )";
			countslq += " and (t.name like :name or t.account like :name )";
			paramsMap.put("name", "%" + name + "%");
		}
		if ((leaderId != null && leaderId > 0)) {
			sql += " and (t.leaderId = :leaderId )";
			countslq += " and (t.leaderId = :leaderId )";
			paramsMap.put("leaderId", leaderId );
		}
		if ((createdBy != null && createdBy > 0)) {
			sql += " and (t.createdBy = :createdBy )";
			countslq += " and (t.createdBy = :createdBy )";
			paramsMap.put("createdBy", createdBy );
		}
		if (!StringUtils.isEmpty(manager)) {
			sql += " and (t.leaderId = :leaderId )";
			countslq += " and (t.leaderId = :leaderId )";
			paramsMap.put("leaderId", Long.parseLong(manager) );
		}
		if (!StringUtils.isEmpty(isDelete)) {
			sql += " and (t.isDelete = :isDelete )";
			countslq += " and (t.isDelete = :isDelete )";
			paramsMap.put("isDelete", Boolean.valueOf(isDelete) );
		}
		if(admin_id!=null&&admin_id>0) {
			sql += " and (t.leaderId != :admin_id )";
			countslq += " and (t.leaderId != :admin_id )";
			paramsMap.put("admin_id", admin_id );
		}
		sql+=" order by t.updatedAt desc,t.createdAt desc ";
		Query query = this.em.createQuery(sql, AkjCallUserVo.class);
		Query cQuery = this.em.createQuery(countslq);
		if (!CollectionUtils.isEmpty(paramsMap)) {
			paramsMap.forEach((k, v) -> {
				query.setParameter(k, v);
				cQuery.setParameter(k, v);
			});
		}
		Long count = (Long) cQuery.getSingleResult();
		List<AkjCallUserVo> list = query.setFirstResult((pageNo - 1) * pageSize).setMaxResults(pageSize)
				.getResultList();
		PageRequest pageRequest = PageUtils.buildPageRequestNoSort(pageNo, pageSize);
		Page<AkjCallUserVo> page = new PageImpl<AkjCallUserVo>(list, pageRequest, count.intValue());
		return page;
	}

}
